package com.zcj.util.coder.java;

import com.zcj.util.UtilString;
import com.zcj.util.coder.CoderUtil;
import com.zcj.util.coder.database.TableType;
import com.zcj.util.coder.java.query.QueryBuilder;

public class JavaCodeBuilder {

	public static JavaCode initJavaCode(Class<?> className) {
		String[] allName = className.getName().split("\\.");
		if (allName.length != 6) {// com.thanone.pm2.entity.us.User
			return null;
		}

		// 表名的前缀
		String tablePrefix = "t_" + allName[2] + "_";
		if (className.isAnnotationPresent(TableType.class)) {
			TableType tt = className.getAnnotation(TableType.class);
			if (UtilString.isNotBlank(tt.prefix())) {
				tablePrefix = tt.prefix();
			}
		}

		JavaCode code = new JavaCode();
		code.setPackageName(allName[0] + "." + allName[1] + "." + allName[2]);// com.thanone.pm2
		code.setModuleName(allName[4]);// us
		code.setClassName(allName[5]);// User
		code.setTableName(tablePrefix + allName[5].toLowerCase());// t_user
		code.setFieldList(CoderUtil.allField(className, true));
		code.setAllFieldList(CoderUtil.allField(className, false));
		code.setQbuilderList(QueryBuilder.initQueryColumnList(className));

		return code;
	}

}
